package com.kyxx.topics.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kyxx.topics.R;
import com.kyxx.topics.adapter.TabFragmentAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VipFragment extends Fragment {
    TabLayout tab_vip;
    ViewPager vp_vip;
    private String[] titles = new String[]{"  卡  ", "  券  "};
    private DisplayImageOptions options;
    View view;
    YouHuiQuanFragment youHuiQuanFragment;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden)
            youHuiQuanFragment.initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_vip, container, false);
        options = new DisplayImageOptions.Builder()
//				// 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.icon_user_default)
//				// 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.icon_user_default)
//				// 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.icon_user_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
//                .displayer(new RoundedBitmapDisplayer(9999))
                .build();
        initView();

        return view;
    }

    private void initView() {
        tab_vip = (TabLayout) view.findViewById(R.id.tab_layout);
        vp_vip = (ViewPager) view.findViewById(R.id.viewpager);
        initData();
    }

    private void initData() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new MembershipCardFragment());
        youHuiQuanFragment = new YouHuiQuanFragment();
        fragments.add(youHuiQuanFragment);
        vp_vip.setAdapter(new TabFragmentAdapter(fragments, titles, getChildFragmentManager(), getActivity()));
        tab_vip.setupWithViewPager(vp_vip);
        tab_vip.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 绑定监听事件
     */
    private void initListener() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
